#!/bin/bash
#log4
echo 'Input a number,Pay attention to the ip address of the lidar:
      0) Install SYS_Drive                      1) BuildAI
      2) BuildSLAM                              3) BuildDrive
      4) Build AI_SLAM_DRIVE                    5) Start sense one Terminal
      6) Start sensors                          7) Start rosbag record
      8) Stop positec.service                   9) Rosnode kill all
     10) Rviz display                          11) Add sense rules to tx2
     12) Start camera                          13) Start serial
     14) Start gpio                            15) Ros workspace
     16) Build update package                  17) Update software
     18) Serive start                          19) tx2 cope work to desktop
     20) RemoveSrc                             21) SlamConfig
'
source FilePath.sh

read x
if [ "$x" == "" ];then
    echo "You no input and the system will exit!"
    exit 5
fi

if [ "$x" == "0" ];then
  cd robot_lib/log4cpp
  chmod 777 configure
  ./configure --build=arm-linux --with-pthreads
  make
  make check
  sudo make install
  sudo ldconfig
  cd ..
  cd ..
  sudo apt-get install libpcap-dev
  sudo apt-get install ros-melodic-rosbridge-suite
  sudo apt-get install ros-melodic-ddynamic-reconfigure
fi

if [ "$x" == "1" ];then
    echo "________________BuildAI________________"
    cd robot_build
    ./BuildAI.sh
    cd ..
fi

if [ "$x" == "2" ] ;then
    echo "________________BuildSLAM________________"
    cd robot_build
    ./BuildSLAM.sh
    cd ..
fi

if [ "$x" == "3" ] ;then
  cd ./robot
  chmod 777 ./src/zvision_ros_driver/zvision_lidar/zvision_lidar_driver/cfg/*
  chmod 777 ./src/zvision_ros_driver/zvision_lidar/zvision_lidar_pointcloud/cfg/*
  catkin_make -DCATKIN_WHITELIST_PACKAGES="zvision_lidar_msgs"
  catkin_make -DCATKIN_WHITELIST_PACKAGES="zvision_lidar_driver" -j4
  catkin_make -DCATKIN_WHITELIST_PACKAGES="zvision_lidar"
  catkin_make -DCATKIN_WHITELIST_PACKAGES="zvision_lidar_pointcloud"
  catkin_make -DCATKIN_WHITELIST_PACKAGES="realsense2_camera" -j4
  catkin_make -DCATKIN_WHITELIST_PACKAGES="camera_pub"
  catkin_make -DCATKIN_WHITELIST_PACKAGES="serial_date"
  catkin_make -DCATKIN_WHITELIST_PACKAGES="gpio_ss"
  catkin_make -DCATKIN_WHITELIST_PACKAGES="fusion_result_pub"
  catkin_make -DCATKIN_WHITELIST_PACKAGES="start_robot"
  catkin_make -DCATKIN_WHITELIST_PACKAGES="auto_slam"
  catkin_make -DCATKIN_WHITELIST_PACKAGES="fusion_rtk_quat"
  catkin_make -DCATKIN_WHITELIST_PACKAGES="gui_rtk"
  cd ..
fi
if [ "$x" == "4" ];then
   echo "________________Build AI+SLAM+DRI________________"
   cd ./robot_build
   ./BuildSrc.sh  
   cd ..
fi
if [ "$x" == "5" ];then
  echo "________________START ROBOT LAUNCH________________"
  echo "123"|sudo -S chmod 777 /dev/ttyGS1
  source ./robot/devel/setup.bash
  roslaunch start_robot start_robot.launch
fi
if [ "$x" == "6" ];then
  if [ -e "/sys/class/gpio/gpio304" ];then
    echo "GPIO 304 exist"
  else 
    echo "GPIO 304 does not exist,make it"
    echo 304 > /sys/class/gpio/export
    sleep 1
    echo out > /sys/class/gpio/gpio304/direction
    sleep 1
    echo in > /sys/class/gpio/gpio304/direction
  fi
  if [ -e "/sys/class/gpio/gpio342" ];then
    echo "GPIO 342 exist"
  else 
    echo "GPIO 342 does not exist,make it"
    echo 342 > /sys/class/gpio/export
    sleep 1
    echo in > /sys/class/gpio/gpio342/direction
    sleep 1
    echo out > /sys/class/gpio/gpio342/direction
    sleep 1
    echo 1 > /sys/class/gpio/gpio342/value
  fi
  source ~/positec/robot/devel/setup.bash
  cd robot_start
  chmod 777 run.sh
  ./run.sh
  cd ..
fi
if [ "$x" == "7" ];then
  echo "________________ROBOT BAG________________"
  cd robot_bag
  ./rosbag_record.sh
  cd ..
fi
 
 
if [ "$x" == "8" ];then
  echo "________________ROSNODE KILL________________"
   cd robot_stop
  chmod 777 stop.sh
  ./stop.sh
  cd ..
fi
if [ "$x" == "9" ];then
  rosnode kill -a
fi
if [ "$x" == "10" ];then
  cd robot/src/start_robot/rviz
  rviz -d rviz.rviz
  cd -
fi
if [ "$x" == "11" ];then
  if [ -e "/etc/udev/rules.d/positec.rules" ];then
    echo "File exist"
    cat /etc/udev/rules.d/positec.rules
  else 
    echo "File does not exist,make it,/etc/udev/rules.d,chown -R root:root positec.rules"
    cd robot_rules
    sudo cp -pf positec.rules /etc/udev/rules.d/
    cat positec.rules
    echo "123"|sudo -S chown -R root:root /etc/udev/rules.d/positec.rules
  fi
fi
if [ "$x" == "12" ];then
  source ~/positec/robot/devel/setup.bash
  roslaunch camera_pub camera_test.launch
fi
if [ "$x" == "13" ];then
  echo "123"|sudo -S chmod 777 /dev/ttyGS1
  echo "123"|sudo -S chmod 777 /dev/pps0
  source ~/positec/robot/devel/setup.bash
  roslaunch serial_date serial_data.launch
fi
if [ "$x" == "14" ];then
  if [ -e "/sys/class/gpio/gpio304" ];then
    echo "GPIO 304 exist"
  else 
    echo "GPIO 304 does not exist,make it"
    echo 304 > /sys/class/gpio/export
    sleep 1
    echo out > /sys/class/gpio/gpio304/direction
    sleep 1
    echo in > /sys/class/gpio/gpio304/direction
  fi
  source ~/positec/robot/devel/setup.bash
  roslaunch gpio_ss gpio.launch
fi
if [ "$x" == "15" ];then
  cd robot/src
  catkin_init_workspace
  cd -
fi
if [ "$x" == "16" ];then
  echo "Robot_update package update.tar.gz.enc replace robot devel src"
  cd robot_update
  bash generate.sh
  cd -
fi
if [ "$x" == "17" ];then
  echo "Robot_update package update.tar.gz.enc replace robot devel src"
  cd robot_update
  bash update.sh
  cd -
fi
if [ "$x" == "18" ];then
  sudo cp /home/nvidia/positec/robot_service/etc/systemd/system/positec.service /etc/systemd/system
  systemctl daemon-reload
  systemctl enable positec.service
fi

if [ "$x" == "19" ];then
  rm -rf ~/positec
  cp -rpf ../positec /home/nvidia/
  sync
  cat ./robot/misc/version
fi

if [ "$x" == "20" ];then
   cd ./robot_build
   ./RemoveSrc.sh
fi

if [ "$x" == "21" ];then
   cp  /home/nvidia/config/viw/bsd_cam_0501.yaml  /home/nvidia/positec/robot/src/VIW-Fusion/config/bsd_0621/
   cp  /home/nvidia/config/viw/evo_cam.yaml  /home/nvidia/positec/robot/src/VIW-Fusion/config/bsd_0621/
   cp  /home/nvidia/config/fusion/camera_intrinsics.yaml  /home/nvidia/positec/robot/src/Global-Sensor-Fusion/config/
fi
